# coding: utf-8

import web
import datetime

import db
import skate
from libs import *

__all__ = ['create', 'show']

class create:

    create_form = form.Form(
            form.Textbox('subject', form.notnull, description="标题"),
            form.Textarea('descr', form.notnull, description="内容"),
            form.Button('submit', type='submit', description='确定'),
            form.Hidden('aid', description=''),
            )

    def GET(self):
        input = web.input(aid=None)
        if input.aid:
            try:
                act = db.get_act(int(input.aid))
            except:
                skate.session.show_msg = True
                skate.session.msg = "活动错误"
                raise                         #XXX for debug
                return web.notfound()
            else:
                f = self.create_form()
                f['aid'].value = act['id']
                return skate.render.discuss_create(f, act['subject'], act['id'])
        else:
            skate.session.show_msg = True
            skate.session.msg = "活动错误"
            return web.notfound()

    def POST(self):
        f = self.create_form()
        if not f.validates():
            skate.session.show_msg = True
            skate.session.msg = "填写不正确"
            return web.seeother('discuss_create?aid=%s' % f['aid'].value)

        uid = skate.session.id
        aid = int(f['aid'].value)
        subject = f['subject'].value
        descr = f['descr'].value
        ip = web.ctx.ip

        seqid = db.new_thread(uid, aid, subject, descr, ip) 

        db.new_news(type=NEWS['newthread'], uid=uid, tid=seqid, aid=aid)

        skate.session.show_msg = True
        skate.session.msg = "发表成功"

        return web.seeother("activity?aid=%d" % aid)

class show:

    post_form = form.Form(
            form.Textarea('descr', form.notnull, description="内容"),
            form.Button('submit', type='submit', description='确定'),
            form.Hidden('aid', description=''),
            form.Hidden('tid', description=''),
            )

    def GET(self):
        input = web.input(aid=None, tid=None)
        if input.aid == None or input.tid == None:
            skate.session.show_msg = True
            skate.session.msg = "讨论不存在"
            web.notfound()

        aid = int(input.aid)
        tid = int(input.tid)

        thread = db.get_thread_detail(tid)
        posts = db.get_posts(tid)
        f = self.post_form()
        f['aid'].value, f['tid'].value = aid, tid
        
        return skate.render.discuss_show(f, thread, posts)


    def POST(self):
        f = self.post_form()
        if not f.validates():
            skate.session.show_msg = True
            skate.session.msg = "回复失败"
            return web.seeother("discuss_show?aid=%s&tid=%s" % (
                f['aid'].value, f['tid'].value))
        
        uid = skate.session.id
        tid = int(f['tid'].value)
        descr = f['descr'].value
        ip = web.ctx.ip

        pid = db.new_post(uid, tid, descr, ip)
        
        db.new_news(type=NEWS['newpost'], pid=pid, tid=tid, uid=uid)

        return web.seeother("discuss_show?aid=%s&tid=%s" % (
            f['aid'].value, f['tid'].value))

class operation:

    def ERROR(self):
        skate.session.show_msg = True
        skate.session.msg = "回复失败"
        raise web.notfound()

    def check_permission(self, aid):
        act = db.get_act(aid)
        if act['uid'] != skate.session.id:
            raise "permission reject"

    def GET(self):
        input = web.input(op=None, aid=None, tid=None)
        if input.op == None or input.aid == None or input.tid== None:
            self.ERROR()

        try:
            aid = int(input.aid)
            tid = int(input.tid)

            self.check_permission(aid)

            if input.op == 'up':
                db.thread_up(tid)
            elif input.op == 'down':
                db.thread_down(tid)
            elif input.op == 'del':
                db.thread_del(tid)
            else:
                raise
        except:
            self.ERROR()

        web.seeother("act_admin?aid=%d" % aid) 

    def POST(self):
        pass

